package StudyCode.summary.mycollection;

import java.util.Iterator;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class LimitsAnIteratorToAGivenCount {
	public static <T> Iterator<T> limitIterator(final Iterator<T> it,
	                                            final long limit) {
		return new Iterator<T>() {
			long left = limit;

			@Override
			public void remove() {
			}

			@Override
			public T next() {
				left--;
				return it.next();
			}

			@Override
			public boolean hasNext() {
				return left > 0 && it.hasNext();
			}
		};
	}
}
